package cn.iocoder.yudao.module.system.service.workstorepallet;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.workstorepallet.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workstorepallet.WorkStorePalletDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.workstorepallet.WorkStorePalletMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 入库托数记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WorkStorePalletServiceImpl implements WorkStorePalletService {

    @Resource
    private WorkStorePalletMapper workStorePalletMapper;

    @Override
    public Long createWorkStorePallet(WorkStorePalletSaveReqVO createReqVO) {
        // 插入
        WorkStorePalletDO workStorePallet = BeanUtils.toBean(createReqVO, WorkStorePalletDO.class);
        workStorePalletMapper.insert(workStorePallet);
        // 返回
        return workStorePallet.getId();
    }

    @Override
    public void updateWorkStorePallet(WorkStorePalletSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkStorePalletExists(updateReqVO.getId());
        // 更新
        WorkStorePalletDO updateObj = BeanUtils.toBean(updateReqVO, WorkStorePalletDO.class);
        workStorePalletMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkStorePallet(Long id) {
        // 校验存在
        validateWorkStorePalletExists(id);
        // 删除
        workStorePalletMapper.deleteById(id);
    }

    @Override
        public void deleteWorkStorePalletListByIds(List<Long> ids) {
        // 校验存在
        validateWorkStorePalletExists(ids);
        // 删除
        workStorePalletMapper.deleteByIds(ids);
        }

    private void validateWorkStorePalletExists(List<Long> ids) {
        List<WorkStorePalletDO> list = workStorePalletMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WORK_STORE_PALLET_NOT_EXISTS);
        }
    }

    private void validateWorkStorePalletExists(Long id) {
        if (workStorePalletMapper.selectById(id) == null) {
            throw exception(WORK_STORE_PALLET_NOT_EXISTS);
        }
    }

    @Override
    public WorkStorePalletDO getWorkStorePallet(Long id) {
        return workStorePalletMapper.selectById(id);
    }

    @Override
    public PageResult<WorkStorePalletDO> getWorkStorePalletPage(WorkStorePalletPageReqVO pageReqVO) {
        return workStorePalletMapper.selectPage(pageReqVO);
    }

}